<?php

/**
 * @filename source_module.php 2013-11-11 02:10:56x
 * @author Huming Xu <huming17@126.com>
 * @version 1.0.0
 * @copyright DZF (c) 2013, Huming Xu
 */
class ctrl_content_info extends content_info {

    public $info_array = array();
    public $page_array = array();
    public $tree_array = array();

    static function &instance() {
        static $object;
        if (empty($object)) {
            $object = new self();
        }
        return $object;
    }

    /*
     * 	取出一条信息
     */

    public function do_detail($info_id = '') {
        global $_G;
        $detail = array();
        $info_id = isset($_REQUEST['id']) ? $_REQUEST['id'] : '';
        $detail = $this->one_info('content_info', 'info_id', $info_id);
        //DEBUG 调试输出
        if ($_G['gp_api']) {
            $return['code'] = '0';
            $detail_format['id'] = $detail['info_id'];
            $detail_format['time'] = date('Y-m-d H:i', $detail['create_dateline']);
            $detail_format['create_dateline'] = $detail['create_dateline'];
            $detail_format['title'] = $detail['title'];
            $detail_format['content'] = trim($detail['content']);
            $detail_format['nav'] = true;
            $return['data'] = $detail_format;
            echo format_data($return, $_G['gp_api'], $_G['gp_mod'], $_G['gp_action'], $_G['gp_do']);
        } else {
            include template('content/info/detail');
        }
    }

    /*
     *  添加系统信息
     */

    public function do_add() {
        global $_G;
        //ini_set("display_errors","On");
        //error_reporting(E_ALL);
        $request_data = isset($_REQUEST['content_info']) && !empty($_REQUEST['content_info']) ? $_REQUEST['content_info'] : '';
        //网校id
        if($_G['member']['role_id']==82){  //网校理员权限
        	$so_id = $this->one_so_info('user_detail', 'user_id', $_G['user_id']);
        }else{
        	$so_id = isset($request_data['so_id']) ? $request_data['so_id'] : '0';
        }
        if ($request_data) {
            if ($_G['gp_api']) {
                $data['info_cateid'] = $request_data['info_cateid'];
                $data['title'] = $request_data['title'];
                $data['content'] = $request_data['content'];
                $data['info_img'] = $request_data['info_img'];
                $data['isfrontpage'] = $request_data['isfrontpage'];
                $data['frontpage_order'] = $request_data['frontpage_order'];
                $data['so_id'] = $so_id;
                $data['create_dateline'] = TIMESTAMP;
                $insert_id = $this->add('content_info', $data);
                if ($insert_id) {
                    $return['code'] = '0';
                } else {
                    $return['code'] = '1';
                }
            }
        } else {
            $return['code'] = '1';
        }
        echo format_data($return, $_G['gp_api'], $_G['gp_mod'], $_G['gp_action'], $_G['gp_do'],'pc','msg_admin','admin');
    }

    /*
     *  编辑系统信息
     */

    public function do_edit() {
        global $_G;
        //ini_set("display_errors","On");
        //error_reporting(E_ALL);
        $request_data = isset($_REQUEST['content_info']) && !empty($_REQUEST['content_info']) ? $_REQUEST['content_info'] : '';
        //网校id
        if($_G['member']['role_id']==82){  //网校理员权限
        	$so_id = $this->one_so_info('user_detail', 'user_id', $_G['user_id']);
        }else{
        	$so_id = isset($request_data['so_id']) ? $request_data['so_id'] : '0';
        }
        if ($request_data['id']) {
            if ($_G['gp_api']) {
                $data['info_cateid'] = $request_data['info_cateid'];
                $data['title'] = $request_data['title'];
                $data['content'] = $request_data['content'];
                $data['info_img'] = $request_data['info_img'];
                $data['isfrontpage'] = $request_data['isfrontpage'];
                $data['frontpage_order'] = $request_data['frontpage_order'];               
                $data['modify_dateline'] = TIMESTAMP;
                if($so_id){
                	$data['so_id'] = $so_id;
                }
                $where = array('info_id' => $request_data['id']);
                $effect_row = $this->edit('content_info', $data, $where);
                if ($effect_row) {
                    $return['code'] = '0';
                } else {
                    $return['code'] = '1';
                }
            }
        } else {
            $return['code'] = '1';
        }
        echo format_data($return, $_G['gp_api'], $_G['gp_mod'], $_G['gp_action'], $_G['gp_do'],'pc','msg_admin','admin');
    }

    /*
     *  删除系统信息
     */

    public function do_delete() {
        global $_G;
        //ini_set("display_errors","On");
        //error_reporting(E_ALL);
        $request_data = isset($_REQUEST['content_info']) && !empty($_REQUEST['content_info']) ? $_REQUEST['content_info'] : '';
        if ($request_data['id']) {
            if ($_G['gp_api']) {
                $where = array('info_id' => $request_data['id']);
                $effect_row = $this->delete("content_info", $where, $limit = 1);
                if ($effect_row) {
                    $return['code'] = '0';
                } else {
                    $return['code'] = '1';
                }
            }
        } else {
            $return['code'] = '1';
        }
        echo format_data($return, $_G['gp_api'], $_G['gp_mod'], $_G['gp_action'], $_G['gp_do'],'pc','msg_admin','admin');
    }

    /*
     *  系统信息列表
     */

    public function do_index() {
    	global $_G;
        $page = max(1, intval($_GET['page']));
        $perpage = $limit = isset($_REQUEST['limit']) ? $_REQUEST['limit'] : 10;
        $start=(($page-1) * $perpage);
        $wheresql = "";
        $keyword = isset($_REQUEST['keyword']) ? $_REQUEST['keyword'] : '';
        if($keyword){
            $wheresql = " AND title LIKE '%".$keyword."%' ";
        }
        //$so_id = isset($_REQUEST['so_id']) ? $_REQUEST['so_id'] : 0;
        $so_id = $this->one_so_info('user_detail', 'user_id', $_G['user_id']);
        $wheresql .= " AND so_id = ".$so_id;
        $info_cateid = isset($_REQUEST['id']) ? $_REQUEST['id'] : '';
        if($info_cateid){
            $wheresql .= " AND info_cateid = '".$info_cateid."' ";
        }
        $orderby = " ORDER BY info_id DESC ";
        //DEBUG 初始化请求请求获取一页列表数据的参数
        $page_condition=array(
            'page' => $page,//int 请求页面 页码
            'limit' => $limit,//int 每页请求个数
            'perpage' => $perpage,//int 每页显示个数
            'wheresql' => $wheresql,//string //条件SQL语句 
            'orderby' => $orderby,//string 排序规则
        );
        //DEBUG 列表数据返回结构
        /*
        $page_result = array(
            //int 返回结果总数
            'total_rows' => $total_rows,
            //array 一页数据数组
            'page_data' => $page_data 
        );
        */
        $page_result = $this->index('content_info',$page_condition);
        //$multipage = multi($page_result['total_rows'], $perpage, $page, "index.php?mod=content&action=info&do=index&keyword=".$keyword);
        //DEBUG 获取各类信息分类名称 开始
        $info_cateid_array = array();
        foreach($page_result['page_data'] AS $key => $value){
            $tmp = array();
            $tmp['id']=$value['info_id'];
            $tmp['time']=date('Y-m-d H:i',$value['create_dateline']);
            $tmp['create_dateline']=$value['create_dateline'];
            $tmp['title']=$value['title'];
            $info_cateid_array[]=$tmp;
        }
        //$content_cate_title_array = $this->get_field_value_by_id('content_cate','info_cateid',$info_cateid_array,'title');
        //DEBUG 获取各类信息分类名称 结束
        $page_result['page_data'] = $info_cateid_array;
        //DEBUG 返回输出
        $api=isset($_REQUEST['api']) ? $_REQUEST['api']:'json';
        $client=isset($_REQUEST['client']) ? $_REQUEST['client']:'wap';
        
        $return['code'] = '0';
        $return['data']=$page_result;
        echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
        
    }

    /*
     *  关于我们左侧菜单
     */

    public function do_about() {
        global $_G;
        $page = max(1, intval($_GET['page']));
        $perpage = $limit = 50;
        $start = (($page - 1) * $perpage);
        $wheresql = "";
        $info_cateid = 1003;
        if ($info_cateid) {
            $wheresql .= " AND info_cateid = '" . $info_cateid . "' ";
        }
        $orderby = " ORDER BY info_id ASC ";
        //DEBUG 初始化请求请求获取一页列表数据的参数
        $page_condition = array(
            'page' => $page, //int 请求页面 页码
            'limit' => $limit, //int 每页请求个数
            'perpage' => $perpage, //int 每页显示个数
            'wheresql' => $wheresql, //string //条件SQL语句 
            'orderby' => $orderby, //string 排序规则
        );
        //DEBUG 列表数据返回结构
        /*
          $page_result = array(
          //int 返回结果总数
          'total_rows' => $total_rows,
          //array 一页数据数组
          'page_data' => $page_data
          );
         */
        $page_result = $this->index('content_info', $page_condition);
        //$multipage = multi($page_result['total_rows'], $perpage, $page, "index.php?mod=content&action=info&do=index&keyword=".$keyword);
        //DEBUG 获取各类信息分类名称 开始
        $info_cateid_array = array();
        foreach ($page_result['page_data'] AS $key => $value) {
            $tmp = array();
            $tmp['id'] = $value['info_id'];
            $tmp['title'] = $value['title'];
            $tmp['create_dateline'] = $value['create_dateline'];
            $info_cateid_array['list'][] = $tmp;
            if ($value['info_id'] == 4) {
                $info_cateid_array['content'] = $value['content'];
            }
        }
        $id = isset($_REQUEST['id']) ? $_REQUEST['id'] : '';
        if ($id) {
            $detail_content_info = $this->one_info('content_info', 'info_id', $id);
            $info_cateid_array['content'] = $detail_content_info['content'];
        }
        //$content_cate_title_array = $this->get_field_value_by_id('content_cate','info_cateid',$info_cateid_array,'title');
        //DEBUG 获取各类信息分类名称 结束
        $page_result['page_data'] = $info_cateid_array;
        //DEBUG 返回输出
        $api = isset($_REQUEST['api']) ? $_REQUEST['api'] : 'json';
        $client = isset($_REQUEST['client']) ? $_REQUEST['client'] : 'wap';
        if ($_G['gp_api']) {
            $return['code'] = '0';
            $return['data'] = $page_result['page_data'];
            echo format_data($return, $_G['gp_api'], $_G['gp_mod'], $_G['gp_action'], $_G['gp_do']);
        } else {
            $multipage = multi($page_result['total_rows'], $perpage, $page, "index.php?mod=ad&action=info&do=index&keyword=" . $keyword);
            include template('famous/teachers/index');
        }
    }

    /*
     *  帮助中心左侧菜单
     */

    public function do_help() {
        global $_G;
        $page = max(1, intval($_GET['page']));
        $perpage = $limit = 80;
        $start = (($page - 1) * $perpage);
        $wheresql = "";
        $info_cateid = "1001,1002";
        if ($info_cateid) {
            $wheresql .= " AND info_cateid IN (" . $info_cateid . ") ";
        }
        $orderby = " ORDER BY info_id DESC ";
        //DEBUG 初始化请求请求获取一页列表数据的参数
        $page_condition = array(
            'page' => $page, //int 请求页面 页码
            'limit' => $limit, //int 每页请求个数
            'perpage' => $perpage, //int 每页显示个数
            'wheresql' => $wheresql, //string //条件SQL语句 
            'orderby' => $orderby, //string 排序规则
        );
        //DEBUG 列表数据返回结构
        /*
          $page_result = array(
          //int 返回结果总数
          'total_rows' => $total_rows,
          //array 一页数据数组
          'page_data' => $page_data
          );
         */
        $page_result = $this->index('content_info', $page_condition);
        //$multipage = multi($page_result['total_rows'], $perpage, $page, "index.php?mod=content&action=info&do=index&keyword=".$keyword);
        //DEBUG 获取各类信息分类名称 开始
        $info_cateid_array = array();
        $info_cateid_array['list'][] = array('id' => '1001', 'title' => '新手指南', 'level' => '1');
        foreach ($page_result['page_data'] AS $key => $value) {
            if ($value['info_cateid'] == 1001) {
                $tmp = array();
                $tmp['id'] = $value['info_id'];
                $tmp['title'] = $value['title'];
                $tmp['create_dateline'] = $value['create_dateline'];
                $tmp['level'] = "2";
                if ($value['info_id'] == 8) {
                    $info_cateid_array['content'] = $value['content'];
                }
                $info_cateid_array['list'][] = $tmp;
            }
        }
        $info_cateid_array['list'][] = array('id' => '1002', 'title' => '常见问题', 'level' => '1');
        foreach ($page_result['page_data'] AS $key => $value) {
            if ($value['info_cateid'] == 1002) {
                $tmp = array();
                $tmp['id'] = $value['info_id'];
                $tmp['title'] = $value['title'];
                $tmp['level'] = "2";
                if ($value['info_id'] == 8) {
                    $info_cateid_array['content'] = $value['content'];
                }
                $info_cateid_array['list'][] = $tmp;
            }
        }
        $id = isset($_REQUEST['id']) ? $_REQUEST['id'] : '';
        if ($id) {
            $detail_content_info = $this->one_info('content_info', 'info_id', $id);
            $info_cateid_array['content'] = $detail_content_info['content'];
        }
        //$content_cate_title_array = $this->get_field_value_by_id('content_cate','info_cateid',$info_cateid_array,'title');
        //DEBUG 获取各类信息分类名称 结束
        $page_result['page_data'] = $info_cateid_array;
        //DEBUG 返回输出
        $api = isset($_REQUEST['api']) ? $_REQUEST['api'] : 'json';
        $client = isset($_REQUEST['client']) ? $_REQUEST['client'] : 'wap';
        if ($_G['gp_api']) {
            $return['code'] = '0';
            $return['data'] = $page_result['page_data'];
            echo format_data($return, $_G['gp_api'], $_G['gp_mod'], $_G['gp_action'], $_G['gp_do']);
        } else {
            $multipage = multi($page_result['total_rows'], $perpage, $page, "index.php?mod=ad&action=info&do=index&keyword=" . $keyword);
            include template('famous/teachers/index');
        }
    }

    /*
     *  编辑推荐公告到首页
     */
    public function do_recommend() {
        global $_G;
        //ini_set("display_errors","On");
        //error_reporting(E_ALL);
        $request_data = isset($_REQUEST['recommend']) && !empty($_REQUEST['recommend']) ? $_REQUEST['recommend'] : '';
        if ($request_data['id']) {
            if ($_G['gp_api']) {
                if($request_data['isfrontpage']==''){
                    $request_data['isfrontpage']=0;
                }
                $data = array('isfrontpage'=>$request_data['isfrontpage']);
                $where = array('info_id'=>$request_data['id']);
                $effect_row = $this->edit('content_info', $data, $where);
                if ($effect_row) {
                    $return['code'] = '0';
                } else {
                    $return['code'] = '1';
                }
            }
        } else {
            $return['code'] = '1';
        }
        echo format_data($return, $_G['gp_api'], $_G['gp_mod'], $_G['gp_action'], $_G['gp_do'],'pc','msg_admin','admin');
    }
    
    /**
     * 接口 AJAX或外部请求检索数据
     *
     * @param $return_type string json/xml.
     *
     * @return print string json/xml.
     */
    public function do_api() {
        $return = '';
        $page = max(1, intval($_GET['page']));
        $perpage = $limit = 20;
        $start = (($page - 1) * $perpage);
        $keyword = isset($_REQUEST['keyword']) ? $_REQUEST['keyword'] : '';
        $return_type = isset($_REQUEST['return_type']) ? $_REQUEST['return_type'] : 'json';
        $wheresql = "";
        if ($keyword) {
            $wheresql = " WHERE title LIKE '%" . $keyword . "%'";
        }
        $orderby = " ORDER BY info_id DESC ";
        //DEBUG 初始化请求请求获取一页列表数据的参数
        $page_condition = array(
            'page' => $page, //int 请求页面 页码
            'limit' => $limit, //int 每页请求个数
            'perpage' => $perpage, //int 每页显示个数
            'wheresql' => $wheresql, //string //条件SQL语句 
            'orderby' => $orderby, //string 排序规则
        );
        //DEBUG 列表数据返回结构
        /*
          $page_result = array(
          //int 返回结果总数
          'total_rows' => $total_rows,
          //array 一页数据数组
          'page_data' => $page_data
          );
         */
        $page_result = $this->api('content_info', $page_condition);
        if ('json' == $return_type) {
            $return = json_encode($page_result);
        } elseif ('xml' == $return_type) {
            $return = array2xml($page_result);
        }
        //DEBUG 接口响应直接输出返回
        echo $return;
    }

}

?>